package hibu.LeiJonGun.customViews;

//Created by Elis Tor Jonsson, with good help from:
//https://code.google.com/p/devoxx-schedule/source/browse/devoxx-android-client/src/net/peterkuterna/android/apps/devoxxsched/ui/widget/FlowLayout.java?name=422c381967&r=422c38196733ba3c54eb44418160e248ee1aea86
//and http://www.parleys.com/#st=5&id=2191&sl=38
//This is a helping class to ActAssignmentArtice when it comes to displaying the article buttons

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class CustomFlowlayoutArticle extends ViewGroup {
	
    private int mHorizontalSpacing;
    private int mVerticalSpacing;
    
    public CustomFlowlayoutArticle(Context context){ //not in use
    	super(context);
    }
    

	public CustomFlowlayoutArticle(Context context, int mHorizontalSpacing, int mVerticalSpacing) {
		super(context);
		this.mHorizontalSpacing = mHorizontalSpacing;
		this.mVerticalSpacing = mVerticalSpacing;
	}
		
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		
		int width = 0;
		int height = getPaddingTop();
		
		int currentWidth = getPaddingLeft();
		int currentHeight = 0;
		
		boolean breakline = false;
		
		final int count = getChildCount();
		for (int i = 0; i < count; i++){
			View child = getChildAt(i);
			LayoutParams lp = (LayoutParams) child.getLayoutParams();
			measureChild(child,widthMeasureSpec,heightMeasureSpec);
			
			if (breakline || currentWidth + child.getMeasuredWidth() > widthSize){
				height += currentHeight + mVerticalSpacing;
				currentHeight = 0;
				if (currentWidth > width) width = currentWidth;
                 currentWidth = getPaddingLeft();	
			}
			
			
			int spacing = mHorizontalSpacing;
			if (lp.spacing > -1){ //override default horizontal spacing
				spacing = lp.spacing;
			}
			
			lp.x = currentWidth + spacing;
			lp.y = height;
			
			currentWidth += child.getMeasuredWidth() + spacing;
			currentHeight = Math.max(currentHeight, child.getMeasuredHeight());
			
			breakline = lp.breakLine;
		}
		
        { //So the bottom article button is shown
            height += currentHeight;
            if (currentWidth > width) width = currentWidth;
        }
		
		width += getPaddingRight();
		height += getPaddingBottom();
		
		setMeasuredDimension(resolveSize(width,widthMeasureSpec),resolveSize(height,heightMeasureSpec));
	}

	@Override
	protected void onLayout(boolean changed, int arg1, int arg2, int arg3, int arg4) {

		final int count = getChildCount();
		for (int i = 0; i < count; i++){
			View child = getChildAt(i);
			LayoutParams lp = (LayoutParams) child.getLayoutParams();
			child.layout(lp.x,lp.y,lp.x + child.getMeasuredWidth(),lp.y + child.getMeasuredHeight());
		}

	}

	@Override
	protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
		return p instanceof LayoutParams;
	}

	@Override
	protected LayoutParams generateDefaultLayoutParams() {
		return new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
	}

	@Override
	public LayoutParams generateLayoutParams(AttributeSet attrs) {
		return new LayoutParams(getContext(),attrs);
	}

	@Override
	protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
		return new LayoutParams(p.width,p.height);
	}

	public static class LayoutParams extends ViewGroup.LayoutParams{
		public boolean breakLine;
		public int spacing = -1;
		
		private int x;
		private int y;
		
		public LayoutParams(int width, int height){
			super(width,height);
		}
		
		
		public LayoutParams(Context context, AttributeSet attrs) {
			super(context, attrs);
		}
		
		
	}
}
